package com.zzg.demo.springbatch.demo.customer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.validator.ValidatingItemProcessor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.util.function.Consumer;

/**
 * 自定义处理器
 * 继承ValidatingItemProcessor和实现ItemProcessor的区别是，前者可以设置数值校验器
 * @param <I>
 * @param <O>
 */
@Slf4j
public class MyConsumerProcessor<I> extends ValidatingItemProcessor<I> {
    // 自定义对传入的数值进行处理的方法
    private final Consumer<I> consumer;

    public MyConsumerProcessor(Consumer<I> consumer) {
        Assert.notNull(consumer, "A function is required");
        this.consumer = consumer;
    }

    @Nullable
    public I process(I item) {
        /**
         * 需要执行super.process(item)才会调用自定义校验器
         */
        this.consumer.accept(super.process(item));
        return item;
    }
}
